/**
 * 描述：供pages/yizheng/newCase.jsp引用
	@Author:yujiaolong
	@time:2016/08/25
 */
$(function(){
	//接收参数
	var param = $("#tabContent > .tab-pane.active").data();
	showCondition(false);//不加载查询条件
	loadImportedCase(param.date);//加载已导入数据
	
	//下一页加载
	$("div[class='pagination'] li a[class='next']").click(function(){
		$("#page").val(parseInt($("#page").val())+1);
	});

	//加载上一页
	$(".previous.paginate_button").click(function(){
		$("#page").val(parseInt($("#page").val())-1);
	});
});

function loadImportedCase(date,type2) {
	var tb_id='tb'+new Date().getTime();
	$('#'+tabId+' #content').html('<table class="table display nowrap" style="width:100%;"  id="'+tb_id+'"style="text-align: center;"><thead><tr>'
			+'<th >年份</th>'
	 		+'<th >月份</th>'
	 		+'<th >病案号</th>'
	 		+'<th >健康卡号</th>'
	 		+'<th >医疗付款方式</th>'
	 		+'<th >住院次数</th>'
	 		+'<th >离院方式</th>'
	 		+'<th >年龄</th>'
	 		+'<th >性别</th>'
	 		+'<th >住院天数</th>'
	 		+'<th >主要诊断代码</th>'
	 		+'<th >主要诊断名称</th>'
	 		+'<th >住院费用总计</th>'
	 		+'<th >医疗费用</th>'
	 		+'<th >护理费用</th>'
	 		+'<th >医技费用</th>'
	 		+'<th >管理费用</th>'
	 		+'<th >药品费用</th>'
	 		+'<th >主治医师</th>'
	 		+'<th >出院科室</th>'
	 		+'<th >入院日期</th>'
	 		+'<th >出院日期</th>'
	 		+'<th >其他诊断</th>'
	 		+'<th >手术信息</th>'
	 		+'<th >手术医生</th>'
	 		+'<th >MDC</th>'
	 		+'<th >DRG</th>'
	 		+'<th >权重</th>'
	 		+'</tr></thead><tbody id = "table"></tbody></table>')
	var year = date.split("-")[0];//年
	var month = date.split("-")[1];//月
	var colunms =[
		{ "data": "year",
			"render": function(obj, type, full){
				return year;
			 }
		},
		{ "data": "month"  ,
			"render": function(obj, type, full){
				return month;
			 }
		},
		{ "data": "MR_NO",
			 "render": function(obj, type, full){
				 var mrNo =full.MR_NO;
				 return "<a href=\"javascript:medicalRecord('"+full.PMAININDEX+"','"+full.PATIENT_NAME+"')\">"+mrNo+"</a>";
			 }
		},
		{ "data": "PMAININDEX"},//健康卡号
		{"data": "PAYMENT","sWidth":"100px",
			 "render": function(obj, type, full) {
				 var payMent=full.PAYMENT;
				 return payMent?payMent:"";
			 }
		},
		{ "data": "IP_COUNT"}, //住院次数
		{"data": "OUT_WAY",
			 "render": function(obj, type, full) {
				 var outWay=full.OUT_WAY;
				 if(outWay == "1"){
					 outWay = "医嘱离院";
				 }else if(outWay == "2"){
					 outWay = "医嘱转院";
				 }else if(outWay == "3"){
					 outWay = "医嘱转社区卫生服务机构/乡镇卫生院";
				 }else if(outWay == "4"){
					 outWay = "非医嘱离院";
				 }else if(outWay == "5"){
					 outWay = "死亡";
				 }else{
					 outWay = "其他";
				 }
				 return outWay;
			 }
		},
		{ "data": "AGE_YEAR" ,
			 "render": function(obj, type, full) {
				 if(full.AGE_YEAR>0){
					 return full.AGE_YEAR;
				 } else if(full.AGE_MONTH>0){
					 return full.AGE_MONTH + '月'+full.AGE_DAYS + '天';
				 } else if(full.AGE_DAYS>0){
					 return full.AGE_DAYS + '天';
				 }
			 }
		},
		{ "data": "sex",//性别
			 "render": function(obj, type, full) {
				 var sex=full.SEX;
				 if(sex == "1"){
					 sex = "男";
				 }else if(sex == "2"){
					 sex = "女";
				 }else{
					 sex = "未知";
				 }
				 return sex;
			 }
		},
		{ "data": "IN_DAYS" },//住院天数
		{ "data": "MAIN_DIAG_CODE" },//主要诊断编码
		{ "data": "MAIN_DIAG_DESC" ,"render": function(obj, type, full) {
	       	 return linefeedText(obj);
	        }
		},//诊断名称（主要诊断描述）
		{ "data": "COSTAMOUNT" },//住院费用总计
		{ "data": "DRG_MED_COST" },//医疗费用
		{ "data": "DRG_NURS_COST" },//护理费用
		{ "data": "DRG_TECH_COST" },//医技费用
		{ "data": "DRG_MANAGE_COST" },//管理费用
		{ "data": "DRG_DRUG_COST" },//药品费用
		{ "data": "GROUPDOCTOR" },//主诊医生
		{ "data": "DEP_NAME" },//出院科室
		{ "data": "IN_DATE"},//入院日期
		{ "data": "OUT_DATE"},//出院日期
		{ "data": "DIAGINFO","render": function(obj, type, full) {
	       	 return linefeedText(obj);
	        }
		},//其他诊断
		{ "data": "OPERINFO","render": function(obj, type, full) {
	       	 return linefeedText(obj);
	        }
		},//手术信息
		{ "data": "OPERDOCTOR" },//手术医师
		{ "data": "MDC_CODE" },//MDC
		{ "data": "DRG_CODE" },//DRGs
		{ "data": "AS_WEIGHT" }//权重
		];
	var ajaxFunction=function (  aoData, fnCallback,oSetting ) {//正在加载
		
		orderColumn=colunms[aoData.order[0].column].data;//获取排序的字段
		orderMethod=aoData.order[0].dir;//获取asc desc
		var iDisplayStart=aoData.start;
		var iDisplayLength=aoData.length;
		var sEcho = 1;
	   	var caseTotalCount = parseInt($('#'+tabId+' #caseTotalCount').val());
	   	 $.ajax( {
	        "dataType": 'json', 
	        "type": "POST", 
	        "url": contextPath + "/DrgsIndexCal/findImportedCaseList.dhc",
	        "data": {
	        	'date':date,
	        	'type2':type2,
	     		'page': iDisplayStart,//开始行数
        		'pageSize':iDisplayLength,
        		'sEcho':sEcho,
        		'caseTotalCount':caseTotalCount,
	       		 'orderColumn':orderColumn,
	     		 'orderMethod':orderMethod
	        },
	        "success": function(resp) {
	        	 var data=[];
	        	 if(resp.success){
            		$('#'+tabId+' #caseTotalCount').val(resp.data.data.iTotalDisplayRecords);//总条数赋值到隐藏域
            	 }
		       	 data.draw=aoData.draw;
		       	 data.recordsTotal=resp.data.data.iTotalRecords;
		       	 data.recordsFiltered=resp.data.data.iTotalDisplayRecords;
		       	 data.data=resp.data.data.aaData;
				 fnCallback(data,oSetting);
				 
				}
	      } );
    }
	var paramsDataTables={ajaxFunction:ajaxFunction,order:[[4,'asc']],columnDefs:[{"orderable": true,"targets": [ 6,8,9,11,12,13,14,15,16,19,20,24,25,26 ] },{"orderable": false,"targets": '_all' }],tableTitle:'病例明细表',scrollX:true,scrollY:'350px',autoWidth:false,orderCellsTop:true,pageLength:50,lengthMenu:[[ 50,100,500, 1000,99999],[ 50,100,500, 1000,'全部']]};
	$('#'+tabId+' #'+tb_id).dataTableServerPaging(colunms,paramsDataTables);
	if(type2){
		$("#"+tabId+" #"+tb_id+"_filter").html("<label>搜索 <input type='text' id='"+tb_id+"_filter_value' title='病案号、出院科室、DRG代码' placeholder='病案号、出院科室、DRGs代码'  value='"+type2+"'></label>")
	}else{
		$("#"+tabId+" #"+tb_id+"_filter").html("<label>搜索 <input type='text' id='"+tb_id+"_filter_value' title='病案号、出院科室、DRG代码' placeholder='病案号、出院科室、DRGs代码'></label>")
	}
	//重写过滤方法
	$("#"+tabId+" #"+tb_id+"_filter_value").keydown(function(e){
		if(e.keyCode==13){
			var type2 = $("#"+tabId+" #"+tb_id+"_filter_value").val();
			if(type2.length==0){
				$('#'+tabId+' #caseTotalCount').val(0);//总条数赋值到隐藏域
			}
			loadImportedCase(date,type2);
		}
	}); 
}

